#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct data
{
	char * name;
	int age;
}student;


student * stu_create(char * name, int name_len, int age)
{
	student * stu = (student *)malloc(sizeof(student));

	stu->name = (char *) malloc (name_len);
	memcpy(stu->name, name, name_len);
	stu->age = age;

	printf("stu_create %p\n", stu);		// 0x2237010		malloc address
	printf("stu_create %p\n", &stu);	// 0x7ffda09192f8	self address

	printf("stu_create %x\n", stu->name);	// 2237030 string value
	printf("stu_create %x\n", &stu->name);	// 2237010 malloc address

	printf("stu_create %p\n", stu->name);	// 0x2237030 string value
	printf("stu_create %p\n", &stu->name);	// 0x2237010 malloc addr

	printf("stu_create %p\n", &stu->age);	// 0x2237018 malloc addr
	printf("stu_create %p\n", stu->age);	// 0xa	value

	return stu;
}

void stu_release(student * stu)
{
	printf("stu_release %p\n", stu);		// 0x2237010
	printf("stu_release %p\n", &stu);		// 0x7ffda0919308

	printf("stu_release %p\n", stu->name);	// 0x2237030
	printf("stu_release %p\n", &stu->name);	// 0x2237010

	printf("stu_release %p\n", &stu->age);	// 0x2237018	
	printf("stu_release %p\n", stu->age);	// 0xa

	free(stu->name);	/* name 后申请，但要先释放 */
	stu->name = NULL;
	free(stu);
}

int main()
{
	char * name = "abc";
	student * stu = stu_create(name, 3, 10);
	char * tmp = stu->name;

	printf("%s %d\n", stu->name, stu->age);	// abc 10

	printf("stu %p\n", stu);			// 0x2237010
	printf("&stu %p\n", &stu);			// 0x7ffda0919328

	printf("main %p\n", stu->name);		// 0x2237030
	printf("main %p\n", &stu->name);	// 0x2237010

	printf("main %p\n", &stu->age);		// 0x2237018
	printf("main %p\n", stu->age);		// 0xa

	stu_release(stu);
	stu = NULL;

	printf("tmp = %s\n", tmp);		// blank

	printf("tmp %p\n", tmp);		// 0x2237030	
	printf("&tmp %p\n", &tmp);		// 0x7ffda0919330	

	printf("free stu %p\n", stu);	// (nil)
	printf("free &stu %p\n", &stu);	// 0x7ffda0919328

	return 0;
}
